<?php

namespace common\models;

use Yii;

/**
 * This is the model class for table "term".
 *
 * @property int $id ID
 * @property int $XXID 学校ID
 * @property string $XQMC 学期名称
 * @property string $XQDM 学期代码
 * @property string $KSRQ 开始日期
 * @property string $JSRQ 结束日期
 *
 * @property School $xX
 * @property TermCourse[] $termCourses
 * @property Course[] $kCs
 */
class Term extends \yii\db\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'term';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['XXID', 'XQMC', 'XQDM', 'KSRQ', 'JSRQ'], 'required'],
            [['XXID'], 'integer'],
            [['KSRQ', 'JSRQ'], 'safe'],
            [['XQMC', 'XQDM'], 'string', 'max' => 20],
            [['XXID'], 'exist', 'skipOnError' => true, 'targetClass' => School::className(), 'targetAttribute' => ['XXID' => 'ID']],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'XXID' => '学校ID',
            'XQMC' => '学期名称',
            'XQDM' => '学期代码',
            'KSRQ' => '开始日期',
            'JSRQ' => '结束日期',
        ];
    }

    /**
     * Gets query for [[XX]].
     *
     * @return \yii\db\ActiveQuery
     */
    public function getXX()
    {
        return $this->hasOne(School::className(), ['ID' => 'XXID'])->inverseOf('terms');
    }

    /**
     * Gets query for [[TermCourses]].
     *
     * @return \yii\db\ActiveQuery
     */
    public function getTermCourses()
    {
        return $this->hasMany(TermCourse::className(), ['XQID' => 'id'])->inverseOf('xQ');
    }

    /**
     * Gets query for [[KCs]].
     *
     * @return \yii\db\ActiveQuery
     */
    public function getKCs()
    {
        return $this->hasMany(Course::className(), ['ID' => 'KCID'])->viaTable('term_course', ['XQID' => 'id']);
    }
}
